package com.yuerlive.rainbow.module.main.presenter;

import com.yuerlive.rainbow.api.HomApi;
import com.yuerlive.rainbow.api.HomeService;
import com.yuerlive.rainbow.base.BasePresenterImpl;
import com.yuerlive.rainbow.entity.BaseData;
import com.yuerlive.rainbow.entity.SignInfo;
import com.yuerlive.rainbow.entity.VersionInfo;
import com.yuerlive.rainbow.rx.RxHelper;
import com.yuerlive.rainbow.rx.RxObserver;

/**
 * Created by xiaoyi on 2017/6/6.
 */

public class MainPresenter extends BasePresenterImpl implements MainContract.Presenter {
    private MainContract.View view;
    private HomeService homeService;

    public MainPresenter(MainContract.View view) {
        this.view = view;
        this.view.setPresenter(this);
        homeService = HomApi.getInstance().homeService;

    }

    @Override
    public void checkVersion() {
        homeService.checkVersion().compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData<VersionInfo>>(this) {
                    @Override
                    protected void onSuccess(BaseData<VersionInfo> versionInfoBaseData) {
                        view.checkVersionReult(versionInfoBaseData.getObject());
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }

    @Override
    public void getSignData() {
      homeService.getSignData().compose(RxHelper.toUI()).
              subscribe(new RxObserver<BaseData<SignInfo>>(this) {
                  @Override
                  protected void onSuccess(BaseData<SignInfo> signInfoBaseData) {
                      view.showSignResult(signInfoBaseData.getObject());
                  }

                  @Override
                  protected void onFail(String message) {

                  }
              });
    }

    @Override
    public void signRequest() {
        homeService.signRequest().compose(RxHelper.toUI())
                .subscribe(new RxObserver<BaseData>(this) {
                    @Override
                    protected void onSuccess(BaseData data) {
                    view.signRequstResult(data.getCode());
                    }

                    @Override
                    protected void onFail(String message) {

                    }
                });
    }
}
